package com.chenyun.boot.web.application;

import com.chenyun.common.service.IShutdownService;
import com.chenyun.common.utils.ThreadUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 应用程序关闭时处理
 * 其他类可以重写DisposableBean的destroy或者使用@PreDestroy;
 * 这里这是做统一处理
 * @author: Lonni
 * @date: 2022/8/8 0008 17:05
 */
@Component
@Slf4j
public class ApplicationExitEvent implements DisposableBean {
    /**
     * 拿到服务类
     */
    @Autowired(required = false)
    private List<IShutdownService> shutdownServices;

    @Override
    public void destroy() throws Exception {
        log.info("ApplicationExitEvent结束事件,开始执行..");
        ThreadUtil.getInstance().shutdownAndAwaitTermination();

        for (IShutdownService shutdownService : shutdownServices) {
            shutdownService.execute();
        }

    }
}
